const http = require('http')
const fs = require('fs')
const url = require('url')
const qString = require('querystring')

let server = http.createServer()

server.listen(7000, () => {
    console.log('端口 7000')
})

server.on('request', function (req, res) {
    console.log('request')
    
    if (req.method == 'GET') {
        // 请求头传参
        const urlObj = url.parse(req.url, true)
        const pathName = urlObj.pathname
        if(pathName == '/user') {
            console.log('---获取到user的id')
            console.log(urlObj.query.id)
            fs.readFile('./index.html', 'utf-8', (err, data) => {
                if(!err){
                    // res.write(data)
                    res.end(data)
                }
            })
        } else {
            console.log('请求图片')
            fs.readFile('./monica.png', function (err, data) {
                res.end(data)
            })
        }
    }else if(req.method == 'POST') {
        // 请求体传参 
        console.log('----post请求---')
        let data = ''
        req.on('data', (d) => {
            console.log(d)
            data += d
        })
        req.on('end', () => {
            console.log(qString.parse(data))
            console.log(1234)
        })
        res.end()

    }
})
